#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "armsdkcli.h"
#include "armsdkerr.h"
#include "armsdkmod.h"
#include "sdkcmdfuncdeclare.h"
#include "armsdkcmdregister.h"

#include "mpu6050.h"
#include "oled.h"
#include "tftlcd.h"
#include "cmdrvfile.h"

ARM_CMD("quit", quit_cmdstr)
{
    if (eCurCmdLevel == CMD_LEVEL_ROOT)
    {
        COMMAND_ERR();
    }

    ARM_CMD_PROMPT_QUIT();

    return SUCCESS;
}

ARM_CMD("exit", exit_cmdstr)
{
    CHR c = '\0';

    while (TRUE)
    {
        PRINTF("    Did you mean exit sdk for ARMv7 (y or n)?\n");
        c = getchar();
        if (c == 'y')
        {
            PRINTF("\n");
            armSdkExit();

            PRINTF(" sdk stop.\n");
            return EXIT;
        }
        else if (c == 'n')
        {
            PRINTF("\n");
            return SUCCESS;
        }
    }
}

ARM_CMD("app STR {enable | disable | get state}", cmdrv_open_close_str)
{
    BOOL state = FALSE;

    if (ARM_SDK_CMD_ARG(argv[1], "enable"))
    {
        return fileOpen(argv[0]);
    }
    else if (ARM_SDK_CMD_ARG(argv[1], "disable"))
    {
        return fileClose(argv[0]);
    }
    else
    {
        state = fileState();
        PRINTF("%s state: %s\n", argv[1], state == TRUE ? "enabled" : "disabled");
    }

    return SUCCESS;
}

ARM_CMD("app STR {write STR | read}", cmdrv_write_read_str)
{
    INT4 i4Ret = 0;
    INT4 i4Inc = 0;
    UINT1 data[8] = {0};

    if (ARM_SDK_CMD_ARG(argv[1], "write"))
    {
        i4Ret = fileWrite(argv[2]);
        if (i4Ret < 0)
        {
            PRINTF("%s write failed\n", argv[0]);
            return i4Ret;
        }
    }
    else if (ARM_SDK_CMD_ARG(argv[1], "read"))
    {
        i4Ret = fileRead(data, 8);
        if (i4Ret < 0)
        {
            PRINTF("%s read failed\n", argv[0]);
            return i4Ret;
        }
        
        PRINTF("val: ");
        for (i4Inc = 0; i4Inc < 8; i4Inc ++)
        {
            PRINTF("%d ", data[i4Inc]);
        }
        PRINTF("\n");
    }

    return SUCCESS;
}

ARM_CMD("mpu6050 [{enable | disable | get state}]", mpu6050_open_close_str)
{
    BOOL state = FALSE;

    if (argc == 0)
    {
        ARM_CMD_PROMPT_ADD("mpu6050");
        return SUCCESS;
    }

    if (ARM_SDK_CMD_ARG(argv[0], "enable"))
    {
        return armDrvMpu6050Enable();
    }
    else if (ARM_SDK_CMD_ARG(argv[0], "disable"))
    {
        return armDrvMpu6050Disable();
    }
    else
    {
        state = armDrvMpu6050State();
        PRINTF("mpu6050 state: %s\n", state == TRUE ? "enabled" : "disabled");
    }

    return SUCCESS;
}

ARM_CMD("show mpu6050 count STR", mpu6050_get_data_str)
{
    INT4 i4num = 0;

    i4num = atoi(argv[0]);
    if (i4num == 0)
    {
        COMMAND_ERR();
    }

    return armDrvMpu6050Output(i4num);

    return SUCCESS;
}

ARM_CMD("oled {enable | disable | get state}", oled_open_close_str)
{
    BOOL state = FALSE;

    if (ARM_SDK_CMD_ARG(argv[0], "enable"))
    {
        return armDrvOledEnable();
    }
    else if (ARM_SDK_CMD_ARG(argv[0], "disable"))
    {
        return armDrvOledDisable();
    }
    else
    {
        state = armDrvOledState();
        PRINTF("oled state: %s\n", state == TRUE ? "enabled" : "disabled");
    }
    return SUCCESS;
}

ARM_CMD("oled get {mpu6050 | rtc} count STR", oled_get_data_str)
{
    INT4 i4num = 0;

    i4num = atoi(argv[1]);
    if (i4num == 0)
    {
        COMMAND_ERR();
    }

    if (ARM_SDK_CMD_ARG(argv[0], "mpu6050"))
    {
        armDrvOledGetMpu6050(i4num);
    }

    return SUCCESS;
}

ARM_CMD("oled {set STR | clear}", oled_set_data_str)
{
    if (ARM_SDK_CMD_ARG(argv[0], "set"))
    {
        armDrvOledWriteStr((CHR *)argv[1]);
    }
    else if (ARM_SDK_CMD_ARG(argv[0], "clear"))
    {
        armDrvOledClear();
    }
    return SUCCESS;
}

ARM_CMD("uart", uart_module_str)
{
    ARM_CMD_PROMPT_ADD("uart");

    return SUCCESS;
}

ARM_CMD("tftlcd {enable | disable | get state}", tftlcd_enable_str)
{
    BOOL state = FALSE;

    if (ARM_SDK_CMD_ARG(argv[0], "enable"))
    {
        return armDrvTftlcdEnable();
    }
    else if (ARM_SDK_CMD_ARG(argv[0], "disable"))
    {
        return armDrvTftlcdDisable();
    }
    else
    {
        state = armDrvTftlcdState();
        PRINTF("tftlcd state: %s\n", state == TRUE ? "enabled" : "disabled");
    }
    return SUCCESS;
}

ARM_CMD("rtc {open | close}", rtc_open_close_str)
{
    return SUCCESS;
}

ARM_CMD("rtc get {time | state}", rtc_get_str)
{
    return SUCCESS;
}

ARM_CMD("rtc set time STR", rtc_set_data_str)
{
    return SUCCESS;
}

INT4 adSDKCmdRegister()
{
    adSDKCmdStrRegister(quit_cmdstr);
    adSDKCmdStrRegister(exit_cmdstr);
    adSDKCmdStrRegister(cmdrv_open_close_str);
    adSDKCmdStrRegister(cmdrv_write_read_str);
    adSDKCmdStrRegister(mpu6050_open_close_str);
    adSDKCmdStrRegister(mpu6050_get_data_str);
    adSDKCmdStrRegister(oled_open_close_str);
    adSDKCmdStrRegister(oled_get_data_str);
    adSDKCmdStrRegister(oled_set_data_str);
    adSDKCmdStrRegister(uart_module_str);
    adSDKCmdStrRegister(tftlcd_enable_str);
    adSDKCmdStrRegister(rtc_open_close_str);
    adSDKCmdStrRegister(rtc_get_str);
    adSDKCmdStrRegister(rtc_set_data_str);

#if DEBUG_MODE
    print_cmdList(&gu1CmdList);
#endif

    return SUCCESS;
}

